package mt.omid.dimomuth.mgm.xmlrpc;


import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import mt.omid.dimomuth.mgm.MgmHandler;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.server.XmlRpcStreamServer;
import org.apache.xmlrpc.webserver.WebServer;


public class XmlRPCServer extends WebServer implements Runnable
{

//    private static int    port = 10020;

    private static Logger logger;
    private int port;
//    private WebServer  webServer;
    MgmHandler hndl;


    public XmlRPCServer( int port, Logger lgr, MgmHandler hndl )
    {
	super( port );
	this.port = port;
	this.logger = lgr;
	this.hndl = hndl;
    }

    protected XmlRpcStreamServer newXmlRpcStreamServer()
    {
        XmlRpcStreamServer server = super.newXmlRpcStreamServer();
        server.setTypeFactory( new ISO8601Factory( server ) );
        return server;
    }

    public void startServer()
    {
	try
	{
//	    webServer = new XmlRPCServer( this.port );
	    XmlRpcServer rpcServer = getXmlRpcServer();

	    VSPOTHandlerMapping vphm = new VSPOTHandlerMapping();
	    vphm.addHandler( "", hndl.getClass() );

	    rpcServer.setHandlerMapping( vphm );

	    XmlRpcServerConfigImpl serverCfg = ( XmlRpcServerConfigImpl ) rpcServer.getConfig();
	    serverCfg.setContentLengthOptional( false );

	    start();
	}
	catch( XmlRpcException e )
	{
	    logger.log( Level.SEVERE, "XmlRPCExcp", e );
	}
	catch( IOException e )
	{
	    logger.log( Level.SEVERE, "IOException", e );
	}
    }
    
}
